home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 62.0 KB | 1,948 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UScripting.cp
- // Copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __USCRIPTING__
- #include "UScripting.h"
- #endif
-
- // MacApp
-
- #ifndef __UCOMMANDHANDLER__
- #include "UCommandHandler.h"
- #endif
-
- #ifndef __UCOREERRORMGR__
- #include "UCoreErrorMgr.h"
- #endif
-
- #ifndef __UCOREGLOBALS__
- #include "UCoreGlobals.h"
- #endif
-
- #ifndef __UCOREUTILITIES__
- #include "UCoreUtilities.h"
- #endif
-
- // #ifndef __UDIALOG__
- // #include "UDialog.h"
- // #endif
-
- #ifndef __UDISPATCHER__
- #include "UDispatcher.h"
- #endif
-
- // #ifndef __UERRORMGR__
- // #include "UErrorMgr.h"
- // #endif
-
- #ifndef __ULISTITERATOR__
- #include "UListIterator.h"
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __USUBSTITUTION__
- #include "USubstitution.h"
- #endif
-
- // Toolbox
-
- #ifndef __AEPACKOBJECT__
- #include <AEPackObject.h>
- #endif
-
- #ifndef __AEREGISTRY__
- #include <AERegistry.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- // ANSI
-
- #if qDebugMsg
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
- #endif
-
- //========================================================================================
- // Static data allocations
- //========================================================================================
-
- TOSADispatcher* TOSADispatcher::fgDispatcher;
-
- //==================================================================================================
- // class TOSADispatcher
- //==================================================================================================
- #undef Inherited
- #define Inherited TObject
-
- #pragma segment ConstructorRes
- MA_DEFINE_CLASS_M1(TOSADispatcher, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::InitUScripting
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void TOSADispatcher::InitUScripting(MA_ClassReference classDesc)
- {
- if (!IsHighLevelEventAware())
- {
- #if qDebug
- DebugStr((StringPtr)"\p###Not HighLevelEventAware! Check SIZE resource.");
- #endif
- ExitToShell();
- }
-
- if (TOSADispatcher::fgDispatcher)
- {
- #if qDebugMsg
- fprintf(stderr, "Scripting already initialized\n");
- #endif
- }
- else
- {
- AEObjectInit();
- qd.randSeed = TickCount();
-
- if (!classDesc)
- {
- #if qDebug
- DebugStr((StringPtr)"\pNULL classDesc for scripting dispatcher");
- #endif
- Failure(minErr, 0);
- }
-
- fgDispatcher = (TOSADispatcher*) classDesc->Call_DefaultConstructor();
- fgDispatcher->IOSADispatcher();
-
- InitUSubstitution();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::TOSADispatcher :
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- TOSADispatcher::TOSADispatcher()
- : fDefaultTarget(NULL),
- fTemporaryTokens(NULL),
- fPendingReplyList(NULL),
- fDispatchUPP(NULL),
- #if qAttachable
- fAttachedScripts(0),
- fPreDispatchUPP(NULL),
- #endif
- fAccessorUPP(NULL),
- fCompareUPP(NULL),
- fCountUPP(NULL),
- fDisposeTokenUPP(NULL),
- fGetMarkTokenUPP(NULL),
- fMarkUPP(NULL),
- fAdjustMarksUPP(NULL),
- fDispatchLevel(0),
- fAEResolveFlags(kAEIDoMinimum | kAEIDoWhose | kAEIDoMarking)
- {
-
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::~TOSADispatcher :
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- TOSADispatcher::~TOSADispatcher()
- {
- // dispose the routine descriptors allocated by this object
-
- // ••• Should remove handlers first!!!
-
- #if qAttachable
- fPreDispatchUPP = DisposeIfRoutineDescriptor(fPreDispatchUPP);
- #endif
- TAppleEvent::fgIdleProc = DisposeIfRoutineDescriptor(TAppleEvent::fgIdleProc);
- fDispatchUPP = DisposeIfRoutineDescriptor(fDispatchUPP);
- fAccessorUPP = DisposeIfRoutineDescriptor(fAccessorUPP);
- fCompareUPP = DisposeIfRoutineDescriptor(fCompareUPP);
- fCountUPP = DisposeIfRoutineDescriptor(fCountUPP);
- fDisposeTokenUPP = DisposeIfRoutineDescriptor(fDisposeTokenUPP);
- fGetMarkTokenUPP = DisposeIfRoutineDescriptor(fGetMarkTokenUPP);
- fMarkUPP = DisposeIfRoutineDescriptor(fMarkUPP);
- fAdjustMarksUPP = DisposeIfRoutineDescriptor(fAdjustMarksUPP);
-
- fTemporaryTokens = (TList *)FreeIfObject(fTemporaryTokens);
- fPendingReplyList = (TEventList *)FreeIfObject(fPendingReplyList);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::IOSADispatcher :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::IOSADispatcher()
- {
- this->IObject();
-
- fTemporaryTokens = NewList();
- fPendingReplyList = new TEventList;
- fPendingReplyList->IEventList();
-
- #if qDebug
- fPendingReplyList->SetEltType("TClientCommand");
- #endif
-
- this->InstallDispatchHandlers();
- this->InstallObjectCallbacks();
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::InstallDispatchHandlers :
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
- // Just in case, this should be in a different segment than the glue it installs
-
- void TOSADispatcher::InstallDispatchHandlers()
- {
- MAVolatileInit(Handle, tableHandle, NULL);
- MAVolatile(SignedByte, savedState);
-
- if (!TAppleEvent::fgIdleProc)
- {
- TAppleEvent::fgIdleProc = NewAEIdleProc(MacAppAppleEventIdleProc);
- FailNIL(TAppleEvent::fgIdleProc);
- }
-
- if (!fDispatchUPP)
- {
- fDispatchUPP = NewAEEventHandlerProc(TOSADispatcher::DispatchHandlerGlue);
- FailNIL(fDispatchUPP);
- }
-
- short numberOfTables = MACount1Resources('aedt'); // count the number of table resources
- FailResError();
-
- FailInfo fi;
- Try(fi)
- {
- for (short tableIndex = 1; tableIndex <= numberOfTables; ++tableIndex)
- {
- tableHandle = MAGet1IndResource('aedt', tableIndex);
- FailNILResource(tableHandle);
- savedState = LockHandle(tableHandle);
- Size tableSize = GetHandleSize(tableHandle);
- FailMemError();
- short tableElements = (short)(tableSize / sizeof(MAEventTableRec));
-
- MAEventTableRec *tablePtr = (MAEventTableRec*)*tableHandle;
-
- for (short eventIndex = 1; eventIndex <= tableElements; ++eventIndex)
- {
- // install the same event handler for all events
- FailOSErr(AEInstallEventHandler(tablePtr->theClass, tablePtr->theID,
- fDispatchUPP, tablePtr->theValue, FALSE));
- tablePtr++;
- }
-
- HSetState(tableHandle, savedState);
- ReleaseResource(tableHandle);
- tableHandle = NULL;
- }
- fi.Success();
- }
- else // Recover
- {
- if (tableHandle)
- {
- HSetState(tableHandle, savedState);
- ReleaseResource(tableHandle);
- }
- fi.ReSignal();
- }
- }
-
- #if qAttachable
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::ScriptAttached :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::ScriptAttached()
- {
- if (++fAttachedScripts == 1)
- InstallPreDispatchHandler();
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::ScriptDetached :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::ScriptDetached()
- {
- #if qDebug
- Assertion(fAttachedScripts >= 0, "\pToo many calls to ScriptDetached");
- #endif
- if (--fAttachedScripts == 0)
- RemovePreDispatchHandler();
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::InstallPreDispatchHandler :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
- // Just in case, this should be in a different segment than the glue it installs
-
- void TOSADispatcher::InstallPreDispatchHandler()
- {
- if (HasAppleScript())
- {
- if (!fPreDispatchUPP)
- {
- fPreDispatchUPP = NewAEEventHandlerProc(TOSADispatcher::PreDispatchHandlerGlue);
- FailNIL(fPreDispatchUPP);
- }
- FailOSErr(AEInstallSpecialHandler(keyPreDispatch, (UniversalProcPtr)fPreDispatchUPP, FALSE));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::RemovePreDispatchHandler :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::RemovePreDispatchHandler()
- {
- if (HasAppleScript() && (fPreDispatchUPP != NULL))
- FailOSErr(AERemoveSpecialHandler(keyPreDispatch, (UniversalProcPtr)fPreDispatchUPP, FALSE));
- }
-
- #endif // qAttachable
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::InstallObjectCallbacks :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
- // Just in case, this should be in a different segment than the glue it installs
-
- void TOSADispatcher::InstallObjectCallbacks()
- {
- // install the universal object accessor
- fAccessorUPP = NewOSLAccessorProc(TOSADispatcher::ObjectAccessorGlue);
- FailOSErr(AEInstallObjectAccessor(typeWildCard, typeWildCard, fAccessorUPP, 0, FALSE));
-
- fCompareUPP = NewOSLCompareProc(TOSADispatcher::CompareObjectsGlue);
- fCountUPP = NewOSLCountProc(TOSADispatcher::CountObjectsGlue);
- fDisposeTokenUPP = NewOSLDisposeTokenProc(TOSADispatcher::DisposeTokenGlue);
- fGetMarkTokenUPP = NewOSLGetMarkTokenProc(TOSADispatcher::GetMarkTokenGlue);
- fMarkUPP = NewOSLMarkProc(TOSADispatcher::MarkObjectGlue);
- fAdjustMarksUPP = NewOSLAdjustMarksProc(TOSADispatcher::AdjustMarksGlue);
-
- FailOSErr(AESetObjectCallbacks(fCompareUPP, fCountUPP, fDisposeTokenUPP,
- fGetMarkTokenUPP, fMarkUPP, fAdjustMarksUPP, NULL));
- }
-
- #if qAttachable
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::PreDispatchHandler :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- OSErr TOSADispatcher::PreDispatchHandler(CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply)
- {
- MScriptableObject *targetObject;
- MAVolatileInit(OSErr, err, noErr);
-
- FailInfo fi;
- Try(fi)
- {
- targetObject = this->GetTargetObject(aCommandNumber, message);
- err = targetObject->HandleOSAEvent(aCommandNumber, message, reply) ? noErr : errAEEventNotHandled;
- fi.Success();
- }
- else // recover
- {
- // if the event is an "exists" event and the target was not found
- // return false instead of an error
-
- if (aCommandNumber == cAEDoObjectsExist && fi.error == errAENoSuchObject)
- ((TAppleEvent *)reply)->WriteBoolean(keyAEResult, FALSE);
- else
- fi.ReSignal();
- }
-
- return err;
- }
-
- #endif // qAttachable
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::DispatchHandler :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- OSErr TOSADispatcher::DispatchHandler( CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply)
- {
- MAVolatileInit(OSErr, err, noErr);
-
- FailInfo fi;
- Try(fi)
- {
- MScriptableObject *targetObject;
- TScriptableObjectList *theObjectList;
- Boolean targetIsContainer = FALSE;
-
- targetObject = this->GetTargetObject(aCommandNumber, message);
-
- if (targetObject->GetOMClass() == cScriptableObjectList)
- {
- FailNonObject(theObjectList = MA_DYNAMIC_CAST(TScriptableObjectList, targetObject));
- targetObject = theObjectList->GetObjectsContainer();
- targetIsContainer = TRUE;
- }
-
- #if qOptimizeSelfSendAevt
- long transactionID = message->GetTransactionID();
- if (targetObject->IsPendingAction(aCommandNumber, transactionID))
- err = errAEEventNotHandled;
- else
- #endif
- {
- // print events directed at objects other than the application
- // need to be handled as cPrint commands.
- if (aCommandNumber == cFinderPrint && targetObject != gDispatcher)
- aCommandNumber = cPrint;
-
- if (targetIsContainer)
- targetObject->DoAEOnContainedObjects(theObjectList, aCommandNumber, message, reply);
- else
- targetObject->HandleScriptCommand(aCommandNumber, message, reply);
- }
- fi.Success();
- }
- else // recover
- {
- // if the event is an "exists" event and the target was not found
- // return false instead of an error
-
- if (aCommandNumber == cAEDoObjectsExist && fi.error == errAENoSuchObject)
- ((TAppleEvent *)reply)->WriteBoolean(keyAEResult, FALSE);
- else
- fi.ReSignal();
- }
-
- return err;
- } // TOSADispatcher::DispatchHandler
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::ReportDispatchError :
- // Note: This function is invoked when an error occurs during processing of an incoming
- // AppleEvent. All calls to subroutines must call resident code. Apple event manager
- // calls are made directly, rather than through TAppleEvent, to eliminate dependencies
- // on non-resident code.
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TOSADispatcher::ReportDispatchError(AppleEvent* messageEvent,
- AppleEvent* replyEvent,
- OSErr dispatchErr,
- long dispatchErrMessage)
- {
- Boolean sentToSelf = FALSE;
- Boolean interactWithUser = FALSE;
- Boolean senderWantsReply = FALSE;
- OSErr err;
-
- if (messageEvent)
- {
- ProcessSerialNumber eventPSN;
- DescType actualType;
- Size actualSize;
- AESendMode sendMode;
-
- err = AEGetAttributePtr(messageEvent, keyAddressAttr, typeProcessSerialNumber,
- &actualType, &eventPSN, sizeof(ProcessSerialNumber), &actualSize);
- if (err == noErr)
- SameProcess(&eventPSN, &gDispatcher->fProcessNumber, &sentToSelf);
-
- err = AEGetAttributePtr(messageEvent, keyInteractLevelAttr, typeShortInteger,
- &actualType, &sendMode, sizeof(short), &actualSize);
- if (err == noErr)
- senderWantsReply = (sendMode & kAEReplyModeMask) != kAENoReply;
- }
-
- if (!sentToSelf && !senderWantsReply)
- {
- // pass NULL for the idling proc. incoming events right now might
- // cause memory problems
- interactWithUser = MAInteractWithUserNoIdleProc() == noErr;
- }
-
- // Put up a dialog if:
- // 1) we are not responding to an event sent by ourself AND
- // 2) the client doesn't expect a reply AND
- // 3) user interaction is acceptable.
-
- if (!sentToSelf && !senderWantsReply && interactWithUser)
- gDispatcher->ShowError(dispatchErr, dispatchErrMessage);
-
- if (replyEvent && (replyEvent->descriptorType != typeNull) && (replyEvent->dataHandle != NULL))
- {
- long failureError = dispatchErr; // needs to be a signed long
- CStr255 errStr;
- AEPutParamPtr(replyEvent, keyErrorNumber, typeLongInteger, &failureError, sizeof(long));
- if (!LookupErrString(dispatchErr, errReasonID, errStr))
- {
- // No error string was found, substitute the generic one.
- CStr255 errNumStr;
- LookupErrString(errGenericErrorWithNum, errReasonID, errStr);
- NumToString(dispatchErr, errNumStr);
- MAParamText("^ENUM", errNumStr);
- MAReplaceText(errStr);
- }
- AEPutParamPtr(replyEvent, keyErrorString, typeChar, &errStr[1], errStr.Length());
- }
- } // TOSADispatcher::ReportDispatchError
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::GetTargetObject :
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- MScriptableObject* TOSADispatcher::GetTargetObject( CommandNumber aCommandNumber,
- TAppleEvent* appleEvent)
- {
- CTempDesc objectDesc;
- OSErr err = noErr;
- MScriptableObject *targetObject = NULL;
-
- // Get the direct object of the event. If the event is a create element event,
- // use the insertion object.
- if (aCommandNumber == cAECreateElement)
- FailOSErr(appleEvent->ReadParameter(keyAEInsertHere, typeWildCard, objectDesc));
- else
- err = appleEvent->ReadParameter(keyDirectObject, typeWildCard, objectDesc);
-
- if (err == errAEDescNotFound)
- targetObject = this->GetDefaultTarget();
- else
- {
- FailOSErr(err);
- if (objectDesc.GetDescriptorType() == typeInsertionLoc)
- {
- CTempDesc insertionDesc;
-
- FailOSErr(AECoerceDesc(objectDesc, typeAERecord, insertionDesc));
- FailOSErr(objectDesc.DisposeDesc());
- insertionDesc.GetKeyDesc(keyAEObject, typeWildCard, objectDesc);
- appleEvent->WriteParameter(keyDirectObject, objectDesc);
- }
-
- targetObject = this->ResolveObjectSpecifier(objectDesc);
- }
- return targetObject;
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::ResolveObjectSpecifier : Ask the OSL to resolve an object specifier.
- // This will initiate a dialog between the OSL and our object resolution callbacks.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- MScriptableObject* TOSADispatcher::ResolveObjectSpecifier(CAEDesc& objectDesc)
- {
- MScriptableObject *result;
-
- if (objectDesc.GetDescriptorType() == typeObjectSpecifier)
- {
- CTempDesc tokenDesc;
-
- FailOSErr(AEResolve(objectDesc, fAEResolveFlags, tokenDesc));
- result = tokenDesc.GetObject();
- }
- else
- result = this->GetDefaultTarget();
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // GetAppleEventCommandNumber: return the commandNumber associated with a specific
- // event class and ID.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- CommandNumber TOSADispatcher::GetAppleEventCommandNumber(AEEventClass eventClass,
- AEEventID eventID)
- {
- AEEventClass localClass = eventClass;
- AEEventID localID = eventID;
- OSErr err;
- long theCommandNumber = 0;
- AEEventHandlerUPP eventHandler = NULL;
-
- err = AEGetEventHandler(eventClass, eventID, &eventHandler, &theCommandNumber, FALSE);
- if (err != noErr)
- theCommandNumber = 0;
-
- return theCommandNumber;
- }
-
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::ObjectAccessor : Handles the AccessContainedObjects OSL callback by
- // asking the container object to return a reference to a specific contained object. If
- // the object was created for object resolution, and requires disposal later, it is added
- // to the temp tokens list.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::ObjectAccessor(DescType desiredClass,
- CAEDesc& container,
- DescType /* containerClass */,
- DescType form,
- CAEDesc& selectionData,
- CAEDesc& value,
- long /* accessorRefcon */)
- {
- MScriptableObject *containerObject = container.GetObject();
- if (containerObject == NULL)
- containerObject = fDefaultTarget;
-
- MScriptableObject *resultObject = NULL;
- resultObject = containerObject->GetContainedObject(desiredClass, form, selectionData);
- if (resultObject == NULL)
- FailOSErr(errAENoSuchObject);
-
- value.PutObject(resultObject);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::CountObjects: Handle the OSL CountObjects callback by asking
- // a container object to count the contained objects of a certain type.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- long TOSADispatcher::CountObjects( DescType desiredType,
- DescType /*containerClass*/,
- CAEDesc& container)
- {
- MScriptableObject *theContainer = container.GetObject();
- return theContainer->CountContainedObjects(desiredType);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::CompareObjects: Handle the OSL CompareObjects callback by asking
- // the target object to compare itself to another object.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- Boolean TOSADispatcher::CompareObjects( DescType whichOperation,
- CAEDesc& targetDesc,
- CAEDesc& thingToCompare)
- {
- MScriptableObject *theTargetObject = targetDesc.GetObject();
- return theTargetObject->CompareScriptableObjects(whichOperation, thingToCompare);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::DisposeToken: The OSL calls this method after object resolution but
- // before the event is processed. After an event is processed, all temporary tokens
- // are disposed.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::DisposeToken(CAEDesc& unneededToken)
- {
- FailOSErr(unneededToken.DisposeDesc());
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::GetMarkToken: Before marking a set of objects, the OSL asks for a
- // token to contain them. MacApp provides a TScriptableObjectList.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::GetMarkToken( CAEDesc& theContainerToken,
- DescType /*containerClass*/,
- CAEDesc& result)
- {
- MScriptableObject *containerObject = theContainerToken.GetObject();
- TScriptableObjectList *theMarkingList = containerObject->MakeNewMarkingList();
- FailNonObject(theMarkingList);
- result.PutObject(theMarkingList);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::MarkObject: OSL is requesting the specified object be marked.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::MarkObject(CAEDesc& theToken,
- CAEDesc& markingListToken,
- long /*index*/)
- {
- MScriptableObject *markingListObject = markingListToken.GetObject();
- TScriptableObjectList *theMarkingList = MA_DYNAMIC_CAST(TScriptableObjectList, markingListObject);
- FailNonObject(theMarkingList);
- MScriptableObject *objectToMark = theToken.GetObject();
- theMarkingList->InsertMarkedObject(objectToMark);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::AdjustMarks:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::AdjustMarks(long newStart,
- long newStop,
- CAEDesc& markToken)
- {
- // Ask the TAppleObjectList marking list to adjust the items it contains.
- MScriptableObject *markObject = markToken.GetObject();
- TScriptableObjectList *theMarkingList = MA_DYNAMIC_CAST(TScriptableObjectList, markObject);
- FailNonObject(theMarkingList);
- theMarkingList->AdjustMarks(newStart, newStop);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::PostPendingReplyCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::PostPendingReplyCommand(TCommand* command)
- {
- fPendingReplyList->Insert(command);
- }
-
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::MatchReplyToPending:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::MatchReplyToPending(TAppleEvent* reply)
- {
- TClientCommand * theMatchedCommand = NULL;
-
- // Higher priority events are at the end of the list.
- CObjectIterator iter(fPendingReplyList, kIterateBackward);
-
- for (TClientCommand * aCommand = (TClientCommand *)iter.FirstObject(); iter.More(); aCommand = (TClientCommand *)iter.NextObject())
- {
- if (aCommand->GetReturnID() == reply->GetReturnID())
- {
- theMatchedCommand = aCommand;
- break;
- }
- }
-
- if (theMatchedCommand)
- {
- fPendingReplyList->Delete(theMatchedCommand);
- theMatchedCommand->ProcessReply(reply);
- theMatchedCommand->fContext->PostCommand(theMatchedCommand);
- }
- else
- FailOSErr(errAEEventNotHandled);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::GetTemporaryTokens
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- TList* TOSADispatcher::GetTemporaryTokens()
- {
- return fTemporaryTokens;
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::AddTemporaryToken: Insert an object into the temp tokens list.
- // Temp tokens will be discarded after the target is resolved and the event is executed.
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::AddTemporaryToken(TObject* tempToken)
- {
- if (fTemporaryTokens->GetEqualItemNo(tempToken) == kEmptyIndex)
- fTemporaryTokens->InsertLast(tempToken);
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::FreeTemporaryTokens:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TOSADispatcher::FreeTemporaryTokens()
- {
- // Frees all the objects in the temp token list.
- if (fTemporaryTokens && (fDispatchLevel < 1))
- {
- FailInfo fi; // swallow errors
- Try(fi)
- {
- fTemporaryTokens->FreeAll();
- fi.Success();
- }
- }
- }
-
- #if qAttachable
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::PreDispatchHandlerGlue : AppleScript does not send the refCon
- // associated with the standard dispatch handler for the event.
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::PreDispatchHandlerGlue(AppleEvent* message,
- AppleEvent* reply,
- long /* info */)
- {
- MAVolatileInit(TAppleEvent*, messageEvent, NULL);
- MAVolatileInit(TAppleEvent*, replyEvent, NULL);
- OSErr dispatchErr = noErr;
- long dispatchErrMessage = 0L;
-
- TOSADispatcher::fgDispatcher->fDispatchLevel++;
-
- FailInfo fi;
- Try(fi)
- {
- AEEventClass eventClass;
- AEEventID eventID;
- CommandNumber eventCommand = 0;
-
- messageEvent = new TAppleEvent;
- messageEvent->InitializeFromMessage(*message, FALSE);
- replyEvent = new TAppleEvent;
- replyEvent->InitializeFromMessage(*reply, FALSE);
-
- eventClass = messageEvent->ReadEventClass();
- eventID = messageEvent->ReadEventID();
- eventCommand = TOSADispatcher::fgDispatcher->GetAppleEventCommandNumber(eventClass, eventID);
-
- dispatchErr = TOSADispatcher::fgDispatcher->PreDispatchHandler(eventCommand, messageEvent, replyEvent);
- fi.Success();
- }
- else
- {
- dispatchErr = fi.error; // Don't propogate the error.
- dispatchErrMessage = fi.message;
- }
-
- if (dispatchErr != noErr && dispatchErr != errAEEventNotHandled)
- TOSADispatcher::fgDispatcher->ReportDispatchError(message, reply, dispatchErr, dispatchErrMessage);
-
- TOSADispatcher::fgDispatcher->fDispatchLevel--;
- TOSADispatcher::fgDispatcher->FreeTemporaryTokens();
-
- FreeIfObject(messageEvent);
- FreeIfObject(replyEvent);
-
- return dispatchErr;
- } // TOSADispatcher::PreDispatchHandlerGlue
-
- #endif // qAttachable
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::DispatchHandlerGlue
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::DispatchHandlerGlue(AppleEvent* message,
- AppleEvent* reply,
- long info)
- {
- MAVolatileInit(TAppleEvent*, messageEvent, NULL);
- MAVolatileInit(TAppleEvent*, replyEvent, NULL);
- OSErr dispatchErr = noErr;
- long dispatchErrMessage = 0L;
- Boolean matchedToPending = FALSE;
- CommandNumber itsCommand = (CommandNumber)info;
-
- TOSADispatcher::fgDispatcher->fDispatchLevel++;
-
- FailInfo fi;
- Try(fi)
- {
- messageEvent = new TAppleEvent;
- messageEvent->InitializeFromMessage(*message, FALSE);
- replyEvent = new TAppleEvent;
- replyEvent->InitializeFromMessage(*reply, FALSE);
-
- if (itsCommand == cAppleEventReply) // handle incoming replies to client commands
- {
- TOSADispatcher::fgDispatcher->MatchReplyToPending(messageEvent);
- matchedToPending = TRUE;
- }
- else
- dispatchErr = TOSADispatcher::fgDispatcher->DispatchHandler(itsCommand, messageEvent, replyEvent);
-
- fi.Success();
- }
- else // recover
- {
- dispatchErr = fi.error; // Do not propogate the error.
- dispatchErrMessage = fi.message;
- }
-
- if (dispatchErr != noErr && dispatchErr != errAEEventNotHandled)
- TOSADispatcher::fgDispatcher->ReportDispatchError(message, reply, dispatchErr, dispatchErrMessage);
-
- TOSADispatcher::fgDispatcher->fDispatchLevel--;
- TOSADispatcher::fgDispatcher->FreeTemporaryTokens();
-
- // delete the message only if it wasn't matched to a pending command
- if (!matchedToPending)
- FreeIfObject(messageEvent);
- FreeIfObject(replyEvent);
-
- return dispatchErr;
- } // TOSADispatcher::DispatchHandlerGlue
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::ObjectAccessorGlue
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::ObjectAccessorGlue(DescType desiredClass,
- AEDesc* container,
- DescType containerClass,
- DescType form,
- AEDesc* selectionData,
- AEDesc* value,
- long accessorRefcon)
- {
- OSErr result = noErr;
-
- CAEDesc containerDesc(container);
- CAEDesc selectionDesc(selectionData);
- CAEDesc valueDesc(value);
-
- FailInfo fi;
- Try(fi)
- {
- TOSADispatcher::fgDispatcher->ObjectAccessor(desiredClass, containerDesc, containerClass,
- form, selectionDesc, valueDesc, accessorRefcon);
- *value = *(AEDesc *)valueDesc;
- fi.Success();
- }
- else
- result = fi.error;
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::CountObjectsGlue
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::CountObjectsGlue( DescType desiredType,
- DescType containerClass,
- AEDesc* container,
- long* result)
- {
- OSErr err = noErr;
- CAEDesc containerDesc(container);
-
- FailInfo fi;
- Try(fi)
- {
- *result = TOSADispatcher::fgDispatcher->CountObjects(desiredType, containerClass, containerDesc);
- fi.Success();
- }
- else
- err = fi.error;
-
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::CompareObjectsGlue
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::CompareObjectsGlue(DescType whichOperation,
- AEDesc* obj1,
- AEDesc* obj2,
- Boolean* result)
- {
- OSErr errResult = noErr;
- CAEDesc object1Desc(obj1);
- CAEDesc object2Desc(obj2);
-
- FailInfo fi;
- Try(fi)
- {
- *result = TOSADispatcher::fgDispatcher->CompareObjects(whichOperation, object1Desc, object2Desc);
- fi.Success();
- }
- else
- errResult = fi.error;
-
- return errResult;
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::DisposeTokenGlue
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::DisposeTokenGlue(AEDesc* unneededToken)
- {
- OSErr result = noErr;
- CAEDesc tokenDesc(unneededToken);
-
- FailInfo fi;
- Try(fi)
- {
- TOSADispatcher::fgDispatcher->DisposeToken(tokenDesc);
- *unneededToken = *(AEDesc *)tokenDesc;
- fi.Success();
- }
- else
- result = fi.error;
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::GetMarkTokenGlue
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::GetMarkTokenGlue( AEDesc* containerToken,
- DescType containerClass,
- AEDesc* result)
- {
- OSErr theErr = noErr;
- CAEDesc containerDesc(containerToken);
- CAEDesc markDesc(result);
-
- FailInfo fi;
- Try(fi)
- {
- TOSADispatcher::fgDispatcher->GetMarkToken(containerDesc, containerClass, markDesc);
- *result = *(AEDesc *)markDesc;
- fi.Success();
- }
- else
- theErr = fi.error;
-
- return theErr;
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::MarkObjectGlue
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::MarkObjectGlue(AEDesc* dToken,
- AEDesc* markToken,
- long index)
- {
- OSErr result = noErr;
- FailInfo fi;
- Try(fi)
- {
- CAEDesc localToken(dToken);
- CAEDesc localMarkToken(markToken);
- TOSADispatcher::fgDispatcher->MarkObject(localToken, localMarkToken, index);
- fi.Success();
- }
- else
- result = fi.error;
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::AdjustMarksGlue
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- pascal OSErr TOSADispatcher::AdjustMarksGlue(long newStart,
- long newStop,
- AEDesc* markToken)
- {
- OSErr result = noErr;
- CAEDesc markDesc(markToken);
-
- FailInfo fi;
- Try(fi)
- {
- TOSADispatcher::fgDispatcher->AdjustMarks(newStart, newStop, markDesc);
- fi.Success();
- }
- else
- result = fi.error;
- return result;
- }
-
- //==================================================================================================
- // Class TSetPropertyCommand
- //==================================================================================================
- #undef Inherited
- #define Inherited TCommand
-
- #pragma segment ConstructorRes
- MA_DEFINE_CLASS_M1(TSetPropertyCommand, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyCommand::TSetPropertyCommand
- //----------------------------------------------------------------------------------------
-
- TSetPropertyCommand::TSetPropertyCommand()
- : fObjectsToSet(NULL),
- fObjectsToFree(NULL),
- fProperty('\?\?\?\?'),
- fOldPropData(),
- fNewPropData()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyCommand::~TSetPropertyCommand
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- TSetPropertyCommand::~TSetPropertyCommand()
- {
- fObjectsToSet = (TList *)FreeIfObject(fObjectsToSet);
- fObjectsToFree = (TList *)FreeListIfObject(fObjectsToFree);
- }
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyCommand::ISetPropertyCommand
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TSetPropertyCommand::ISetPropertyCommand(DescType theProperty,
- TList* theObjectsToSet,
- TAppleEvent* message,
- TAppleEvent* /* reply */)
- {
- // We take charge of the temporary objects in the disposal list so we can use them
- // during execution of this command.
- // We'll free these when we commit.
- TList * theDisposalList = TOSADispatcher::fgDispatcher->GetTemporaryTokens();
- fObjectsToFree = (TList *)theDisposalList->Clone();
- theDisposalList->DeleteAll();
- // We'll clone the list so the caller can free it immediately.
- fObjectsToSet = (TList *)theObjectsToSet->Clone();
- fProperty = theProperty;
- CommandNumber cmdNum;
- Boolean canUndo;
- Boolean causesChange;
- TObject * theFirstObject = theObjectsToSet->At(1);
- FailNIL(theFirstObject);
-
- // Get the info for this command from the object.
- TCommandHandler* itsContext = NULL;
- ((MScriptableObject*)theFirstObject)->GetSetPropertyInfo(theProperty, cmdNum, canUndo, causesChange, itsContext);
- this->ICommand(cmdNum, itsContext, canUndo, causesChange, itsContext);
-
- fOldPropData = NULL;
-
- CAEDesc newPropData;
- message->ReadParameter(keyAEData, typeWildCard, newPropData);
- fNewPropData = newPropData;
- }
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyCommand::DoIt
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TSetPropertyCommand::DoIt()
- {
- CAEDesc theOldPropList;
- if (fCanUndo)
- {
- theOldPropList.CreateList();
-
- CObjectIterator iter(fObjectsToSet);
- for (TObject * anObject = iter.FirstObject(); iter.More(); anObject = iter.NextObject())
- {
- CTempDesc thePropData;
- ((MScriptableObject*)anObject)->GetObjectProperty(thePropData, fProperty, CAEDesc::fgNullDesc);
- FailOSErr(AEPutDesc(theOldPropList, 0, thePropData));
- }
- }
- fOldPropData = theOldPropList;
-
- this->RedoIt();
- }
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyCommand::UndoIt
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TSetPropertyCommand::UndoIt()
- {
- CAEDesc theOldPropData(fOldPropData);
- Assertion(theOldPropData.GetDataHandle() != NULL, "Trying to undo with no data!");
- CObjectIterator iter(fObjectsToSet);
- short listIndex = 1;
- for (TObject * anObject = iter.FirstObject(); iter.More(); anObject = iter.NextObject())
- {
- CTempDesc theOldPropDesc;
- DescType theActualKey;
- FailOSErr(AEGetNthDesc(theOldPropData, listIndex++, typeWildCard, &theActualKey, theOldPropDesc));
- ((MScriptableObject*)anObject)->SetObjectProperty(theOldPropDesc, fProperty);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyCommand::RedoIt
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TSetPropertyCommand::RedoIt()
- {
- CAEDesc thePropData(fNewPropData);
- CObjectIterator iter(fObjectsToSet);
- for (TObject * anObject = iter.FirstObject(); iter.More(); anObject = iter.NextObject())
- ((MScriptableObject*)anObject)->SetObjectProperty(thePropData, fProperty);
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor
- //----------------------------------------------------------------------------------------
- #undef Inherited
- #define Inherited TObject
-
- #pragma segment ConstructorRes
- MA_DEFINE_CLASS_M2(TPropertyAccessor, Inherited, MScriptableObject);
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::TPropertyAccessor
- //----------------------------------------------------------------------------------------
-
- TPropertyAccessor::TPropertyAccessor()
- : MScriptableObject(cProperty)
- {
- } // TPropertyAccessor::TPropertyAccessor
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TPropertyAccessor::~TPropertyAccessor()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::IPropertyAccessor
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TPropertyAccessor::IPropertyAccessor(MScriptableObject* whichObject,
- DescType whichProperty)
- {
- // Records which object and property to act on.
- IObject();
- fWhichObject = whichObject;
- fWhichProperty = whichProperty;
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::GetCommandContext
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- TCommandHandler* TPropertyAccessor::GetCommandContext(const CommandNumber aCommandNumber) const
- {
- TCommandHandler *commandHandler = NULL;
-
- if (fWhichObject)
- {
- commandHandler = MA_DYNAMIC_CAST(TCommandHandler, fWhichObject);
- if (commandHandler)
- commandHandler = commandHandler->GetContext(aCommandNumber);
- }
-
- if (!commandHandler)
- commandHandler = MScriptableObject::GetCommandContext(aCommandNumber);
-
- return commandHandler;
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::DoScriptCommand
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TPropertyAccessor::DoScriptCommand(CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply)
- {
- // Handles GetData and SetData for this property.
- FailInfo fi;
- Try(fi)
- {
- switch (aCommandNumber)
- {
- case cAEGetData:
- {
- CTempDesc requestedType;
- if (message->HasParameter(keyAERequestedType))
- message->ReadParameter(keyAERequestedType, typeWildCard, requestedType);
-
- CTempDesc thePropertyValue;
- fWhichObject->GetObjectProperty(thePropertyValue, fWhichProperty, requestedType);
- reply->WriteParameter(keyAEResult, thePropertyValue);
- break;
- }
- case cAESetData:
- {
- TList * aOneObjectList = NewList();
- aOneObjectList->InsertLast((TObject*)fWhichObject);
- TSetPropertyCommand * theCmd = new TSetPropertyCommand;
- theCmd->ISetPropertyCommand(fWhichProperty, aOneObjectList, message, reply);
- aOneObjectList = (TList *)FreeIfObject(aOneObjectList);
- theCmd->Process();
- break;
- }
- default:
- MScriptableObject::DoScriptCommand(aCommandNumber, message, reply);
- break;
- }
- fi.Success();
- }
- else
- {
- reply->WriteOSError(fi.error);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::CompareScriptableObjects
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- Boolean TPropertyAccessor::CompareScriptableObjects(DescType operation,
- const CAEDesc& thingToCompare)
- {
- // Compares the properties.
- return fWhichObject->CompareProperties(fWhichProperty, operation, thingToCompare);
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::SetObjectProperty
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TPropertyAccessor::SetObjectProperty(const CAEDesc& thePropertyValue,
- DescType whichProperty)
- {
- fWhichObject->SetObjectProperty(thePropertyValue, whichProperty);
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::GetObjectProperty
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- Boolean TPropertyAccessor::GetObjectProperty(CAEDesc& thePropertyValue,
- DescType whichProperty,
- const CAEDesc& desiredType)
- {
- return fWhichObject->GetObjectProperty(thePropertyValue, whichProperty, desiredType);
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::DoAEGetDataSize
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TPropertyAccessor::DoAEGetDataSize(TAppleEvent* message,
- TAppleEvent* reply)
- {
- CTempDesc requestedType;
- if (message->HasParameter(keyAERequestedType))
- message->ReadParameter(keyAERequestedType, typeWildCard, requestedType);
-
- CTempDesc thePropertyValue;
- fWhichObject->GetObjectProperty(thePropertyValue, fWhichProperty, CAEDesc::fgNullDesc);
- long theSize = thePropertyValue.GetDataSize();
- reply->WriteLong(keyAEResult, theSize);
- }
-
- #if qAttachable
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor::HandleOSAEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- Boolean TPropertyAccessor::HandleOSAEvent( CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply)
- {
- message->WriteType(keyAEProperty, fWhichProperty);
- return fWhichObject->HandleOSAEvent(aCommandNumber, message, reply);
- }
-
- #endif // qAttachable
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList
- //----------------------------------------------------------------------------------------
- #undef Inherited
- #define Inherited TList
-
- #pragma segment ConstructorRes
- MA_DEFINE_CLASS_M2(TScriptableObjectList, Inherited, MScriptableObject);
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::TScriptableObjectList
- //----------------------------------------------------------------------------------------
-
- TScriptableObjectList::TScriptableObjectList(): MScriptableObject(cScriptableObjectList)
- {
- } // TScriptableObjectList::TScriptableObjectList
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TScriptableObjectList::~TScriptableObjectList()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::IScriptableObjectList
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TScriptableObjectList::IScriptableObjectList(MScriptableObject* itsContainer)
- {
- // Initializes the list and records the container.
- IList();
- fContainer = itsContainer;
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::DoAECountElements
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TScriptableObjectList::DoAECountElements(TAppleEvent* /*message*/,
- TAppleEvent* reply)
- {
- // If we say "get number of (windows whose name contains "untitled")" OSL builds a
- // TAppleObjects list of TWindows that satisfies the whose test then calls this method.
- reply->WriteLong(keyAEResult, this->GetSize());
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::DoScriptCommand
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TScriptableObjectList::DoScriptCommand(CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply)
- {
- if (aCommandNumber == cAECountElements)
- MScriptableObject::DoScriptCommand(aCommandNumber, message, reply);
- else
- {
- CTempDesc theReplyList;
- theReplyList.CreateList();
- // Calls DoScriptCommand for all the objects in the list.
- CObjectIterator iter(this);
- for (TObject * anObject = iter.FirstObject(); iter.More(); anObject = iter.NextObject())
- {
- ((MScriptableObject*)anObject)->DoScriptCommand(aCommandNumber, message, reply);
- CTempDesc theEventReply;
- if (reply->HasParameter(keyAEResult))
- {
- reply->ReadParameter(keyAEResult, typeWildCard, theEventReply);
- FailOSErr(AEPutDesc(theReplyList, 0, theEventReply));
- }
- }
- if (reply->fMessage.dataHandle)
- reply->WriteParameter(keyAEResult, theReplyList);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::InsertMarkedObject
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TScriptableObjectList::InsertMarkedObject(MScriptableObject* theObjectMarked)
- {
- // Adds the marked object to the list
- this->InsertLast((TObject*)theObjectMarked);
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::AdjustMarks
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TScriptableObjectList::AdjustMarks(long newStart,
- long newStop)
- {
- // The OSL may occasionally change its mind about which objects should be marked.
- // This routine makes the adjustments.
- long numToDelete = this->GetSize() - newStop;
- if (numToDelete > 0)
- this->DeleteElementsAt(newStop + 1, numToDelete);
- numToDelete = newStart - 1;
- if (numToDelete > 0)
- this->DeleteElementsAt(1, numToDelete);
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::GetObjectsContainer
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- MScriptableObject* TScriptableObjectList::GetObjectsContainer()
- {
- return fContainer;
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::IsPropertyList
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- Boolean TScriptableObjectList::IsPropertyList()
- {
- // How do we know if this is a list of properties and not of objects?
- // We could walk down the list and make sure each element was a TPropertyAccessor,
- // but that adds overhead for large lists.
- // I haven't been able to think of a situation where objects and properties are operated
- // on at the same time and would be in the same list, so lets just check the first one.
- // I believe that even complex object specifiers can only refer to objects of the same class
- // and in the same container.
-
- TObject * theFirstObject = NULL;
- if (fSize > 0)
- theFirstObject = (TObject *)this->At(1);
- return (theFirstObject) && (((MScriptableObject*)theFirstObject)->GetOMClass() == cProperty);
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::GetContainedObject
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- MScriptableObject* TScriptableObjectList::GetContainedObject(DescType desiredType,
- DescType selectionForm,
- const CAEDesc& selectionData)
- {
- // This method is needed when a complex object specifier has resolved into a list of objects,
- // but then actually wants a property from each one.
- // We handle this by building up a list of contained objects from each object in this list.
-
- MAVolatileInit(TScriptableObjectList*, theNewList, new TScriptableObjectList);
- MScriptableObject * theContainer = this->GetObjectsContainer();
- theNewList->IScriptableObjectList(theContainer);
- FailInfo fi;
- Try(fi)
- {
- {
- CObjectIterator iter(this);
- for (TObject * anObject = iter.FirstObject(); iter.More(); anObject = iter.NextObject())
- {
- MScriptableObject *theResult = ((MScriptableObject*)anObject)->GetContainedObject(desiredType, selectionForm, selectionData);
- if (theResult == NULL)
- FailOSErr(errAENoSuchObject);
- theNewList->InsertLast((TObject*)theResult);
- }
- TOSADispatcher::fgDispatcher->AddTemporaryToken(theNewList);
- }
- fi.Success();
- }
- else
- {
- FreeIfObject(theNewList);
- fi.ReSignal();
- }
- return theNewList;
- }
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList::CountContainedObjects
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- long TScriptableObjectList::CountContainedObjects(DescType desiredType)
- // Adds up CountContainedObjects for all objects in the list.
- {
- long result = 0;
- CObjectIterator iter(this);
- for (TObject * anObject = iter.FirstObject(); iter.More(); anObject = iter.NextObject())
- result += ((MScriptableObject*)anObject)->CountContainedObjects(desiredType);
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyEvent
- //----------------------------------------------------------------------------------------
- #undef Inherited
- #define Inherited TAppleEvent
-
- #pragma segment ConstructorRes
- MA_DEFINE_CLASS_M1(TSetPropertyEvent, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyEvent::TSetPropertyEvent
- //----------------------------------------------------------------------------------------
-
- TSetPropertyEvent::TSetPropertyEvent()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyEvent destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TSetPropertyEvent::~TSetPropertyEvent()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyEvent::ISetPropertyEvent
- //----------------------------------------------------------------------------------------
- #pragma segment AEInitialize
-
- void TSetPropertyEvent::ISetPropertyEvent(const CAEDesc& itsAddress,
- long itsSendingMode,
- MScriptableObject* objectToSet,
- DescType propType)
- {
- // Initializes the event with the recipient, sending mode, the object you want to set and the
- // property you want to modify. Creates an object specifier for the object's property and
- // sticks it in the direct object.
- CTempDesc propertyToSet;
- CAEDesc localAddress = itsAddress;
- FailInfo fi;
- Try(fi)
- {
- this->IAppleEvent(kAECoreSuite, kAESetData, (AEAddressDesc&)localAddress, itsSendingMode);
- CAEDesc objectSpecifier;
- CAEDesc theProperty;
- objectToSet->MakeObjectSpecifier(objectSpecifier, objectToSet->GetSpecifierForm());
- theProperty.PutType(propType);
- FailOSErr(CreateObjSpecifier(cProperty, objectSpecifier, formPropertyID, theProperty, TRUE, propertyToSet));
- this->WriteParameter(keyDirectObject, propertyToSet);
- fi.Success();
- }
- else
- {
- this->Free();
- fi.ReSignal();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor
- //----------------------------------------------------------------------------------------
- #undef Inherited
- #define Inherited TObject
-
- #pragma segment ConstructorRes
- MA_DEFINE_CLASS_M2(TMenuAccessor, Inherited, MScriptableObject);
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor::TMenuAccessor
- //----------------------------------------------------------------------------------------
-
- TMenuAccessor::TMenuAccessor()
- : MScriptableObject(cMenu)
- {
- } // TMenuAccessor::TMenuAccessor
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TMenuAccessor::~TMenuAccessor()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor::IMenuAccessor
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TMenuAccessor::IMenuAccessor(MenuRef theMenu, short theIndex)
- {
- IObject();
- fMenu = theMenu;
- fIndex = theIndex;
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor::GetObjectProperty
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- Boolean TMenuAccessor::GetObjectProperty(CAEDesc& thePropertyValue,
- DescType whichProperty,
- const CAEDesc& desiredType)
- {
- Boolean hasProperty = TRUE;
- FailInfo fi;
- Try(fi)
- {
- switch (whichProperty)
- {
-
- case pName:
- {
- CStr255 theMenuName = (CStr255&)(**fMenu).menuData;
- thePropertyValue.PutString(theMenuName);
- }
- break;
-
- case pIndex:
- {
- thePropertyValue.PutLong(fIndex);
- }
- break;
-
- case pMenuID:
- {
- thePropertyValue.PutLong((long)(**fMenu).menuID);
- }
- break;
-
- default:
- hasProperty = MScriptableObject::GetObjectProperty(thePropertyValue, whichProperty, desiredType);
- break;
- }
- fi.Success();
- }
- else
- {
- hasProperty = FALSE;
- }
- return hasProperty;
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor::SetObjectProperty:
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TMenuAccessor::SetObjectProperty(const CAEDesc& thePropertyValue,
- DescType whichProperty)
- {
- switch (whichProperty)
- {
- case pName:
- case pMenuID:
- FailOSErr(errAECantSetReadOnly);
- break;
-
- default:
- MScriptableObject::SetObjectProperty(thePropertyValue, whichProperty);
- break;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor::GetContainedObject
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- MScriptableObject* TMenuAccessor::GetIndContainedObject(DescType desiredType,
- long index)
- {
- if (desiredType == cMenuItem)
- {
- // Create a menu item accessor to handle events for this menu item
- TMenuItemAccessor* theAccessor = new TMenuItemAccessor;
- theAccessor->IMenuItemAccessor(fMenu, (short)index);
- TOSADispatcher::fgDispatcher->AddTemporaryToken(theAccessor);
- return theAccessor;
- }
- else
- return MScriptableObject::GetIndContainedObject(desiredType, index);
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor::CountContainedObjects
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- long TMenuAccessor::CountContainedObjects(DescType desiredType)
- // Returns the number of menu item in a menu.
- {
- if (desiredType == cMenuItem)
- return CountMItems(fMenu);
- else
- return MScriptableObject::CountContainedObjects(desiredType);
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuItemAccessor
- //----------------------------------------------------------------------------------------
- #undef Inherited
- #define Inherited TObject
-
- #pragma segment ConstructorRes
- MA_DEFINE_CLASS_M2(TMenuItemAccessor, Inherited, MScriptableObject);
-
- //----------------------------------------------------------------------------------------
- // TMenuItemAccessor::TMenuItemAccessor
- //----------------------------------------------------------------------------------------
-
- TMenuItemAccessor::TMenuItemAccessor(): MScriptableObject(cMenuItem)
- {
- } // TMenuItemAccessor::TMenuItemAccessor
-
- //----------------------------------------------------------------------------------------
- // TMenuItemAccessor destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TMenuItemAccessor::~TMenuItemAccessor()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuItemAccessor::IMenuItemAccessor
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TMenuItemAccessor::IMenuItemAccessor(MenuRef theMenu, short theItem)
- {
- IObject();
- fMenu = theMenu;
- fMenuItem = theItem;
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuItemAccessor::GetObjectProperty
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- Boolean TMenuItemAccessor::GetObjectProperty(CAEDesc& thePropertyValue,
- DescType whichProperty,
- const CAEDesc& desiredType)
- {
- Boolean hasProperty = TRUE;
- FailInfo fi;
- Try(fi)
- {
- switch (whichProperty)
- {
-
- case pName:
- {
- CStr255 theItemName;
- GetMenuItemText(fMenu, fMenuItem, theItemName);
- thePropertyValue.PutString(theItemName);
- }
- break;
-
- case pItemNumber:
- case pIndex:
- {
- thePropertyValue.PutLong(fMenuItem);
- }
- break;
-
- case pEnabled:
- {
- Boolean isEnabled = FALSE;
- if ((fMenuItem > 0) && (fMenuItem < 32))
- isEnabled = ((((*fMenu)->enableFlags >> fMenuItem) & 0x01) != 0);
- else
- isEnabled = TRUE;
- thePropertyValue.PutBoolean(isEnabled);
- }
- break;
-
- default:
- hasProperty = MScriptableObject::GetObjectProperty(thePropertyValue, whichProperty, desiredType);
- break;
- }
- fi.Success();
- }
- else
- {
- hasProperty = FALSE;
- }
- return hasProperty;
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuItemAccessor::SetObjectProperty:
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TMenuItemAccessor::SetObjectProperty(const CAEDesc& thePropertyValue,
- DescType whichProperty)
- {
- switch (whichProperty)
- {
- case pName:
- case pItemNumber:
- case pEnabled:
- FailOSErr(errAECantSetReadOnly);
- break;
-
- default:
- MScriptableObject::SetObjectProperty(thePropertyValue, whichProperty);
- break;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TMenuItemAccessor::DoAEOpen
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- void TMenuItemAccessor::DoAEOpen(TAppleEvent* /* message */ ,
- TAppleEvent* /* reply */)
- {
- gDispatcher->MenuEvent(((long)(**fMenu).menuID << 16) | fMenuItem);
- }
-
- //----------------------------------------------------------------------------------------
- // End of UScripting.cp
-
- #pragma segment Inline
-